﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using WR.BL.BE;
using WR.BL.BCC;

namespace WR.UI.MobWebApp
{
    public partial class print : System.Web.UI.Page
    {
        private UserBE _objUsr;
        private void checkLogin()
        {
            lblErrorMsg.InnerHtml = string.Empty;
            _objUsr = (UserBE)Session["UserBE"];
            if (_objUsr == null)
            {
                Response.Redirect("login.aspx");
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                //this.checkLogin();
                if (!Page.IsPostBack)
                {
                    string pNumDoc = Request.QueryString["nd"];
                    if (!string.IsNullOrEmpty(pNumDoc))
                    {
                        SalesOrderBE objSO = null;
                        string retval = SalesOrderBC.getInstance(Session.SessionID).read(pNumDoc, out objSO);
                        if (!string.IsNullOrEmpty(retval))
                        {
                            throw new Exception(retval);
                        }
                        else if (objSO == null)
                        {
                            throw new Exception("El documento especificado no existe.");
                        }
                        else
                        {
                            lblCabecera.InnerHtml = objSO.ClaseDocT + " " + objSO.NumDoc;
                            lblCliente.InnerHtml = objSO.Cliente.Descripcion;
                            lblRuc.InnerHtml = objSO.Cliente.Ruc;
                            lblPago.InnerHtml = objSO.CondPago;
                            lblMoneda.InnerHtml = objSO.Moneda;
                            lblFecha.InnerHtml = objSO.FechaDoc;

                            lvDetalle.DataSource = objSO.Detalle;
                            lvDetalle.DataBind();

                            HtmlGenericControl lblTotal = (HtmlGenericControl)lvDetalle.FindControl("lblTotal");
                            lblTotal.InnerHtml = objSO.Total.ToString("0,0.00");
                        }
                    }
                    else
                    {
                        throw new Exception("Operación inválida.");
                    }
                }
                else
                {
                    throw new Exception("Operación inválida.");
                }
            }
            catch (Exception ex)
            {
                lblErrorMsg.InnerHtml = ex.Message;
            }
        }
    }
}
